<?php
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

$val = $_REQUEST["val"];
$tblname = $_REQUEST["tblname"];
$payment_method = $_REQUEST["payment_method"];
//echo $val, $tblname;

$val1 = explode(",", $val);


if (is_array($val1) && count($val1) > 1) {
//    echo "Y";
    $dis_multi = true;
//    echo count($val1);
} else {
//    echo "N";
}

$val1 = implode("','", $val1);

switch ($tblname) {
    case "tbl_pet":
        $sql = "SELECT SUM(net) AS net, vender_name "
                . " FROM tbl_pet, v_vender "
                . " WHERE pet_id IN('$val1') "
                . " AND tbl_pet.receiver = v_vender.vender_id "
                . " GROUP BY vender_name ";

        break;

    case "tbl_petout":
        $sql = "SELECT SUM(amount) AS net, vender_name "
                . " FROM tbl_petout, v_vender "
                . " WHERE petout_id IN('$val1') "
                . " AND tbl_petout.vender_id = v_vender.vender_id "
                . " GROUP BY vender_name ";

        break;
    
    case "tbl_petition":
        $sql = "SELECT SUM(inpNet) AS net "
                . " FROM tbl_petition "
                . " WHERE p_id IN('$val1') ";
        break;
    case "tbl_salary":
//        $sql = "SELECT SUM(salary) AS net "
//                . " FROM tbl_salary "
//                . " WHERE p_id IN('$val1') ";

        $sql = "SELECT sum(salary) AS net "
                . " FROM tbl_salary, tbl_payment, expenses_lvl1, expenses_lvl2 "
                . " WHERE approved_status = 'อนุมัติ' "
                . " AND pet_id IN('$val1') "
                . " AND tbl_salary.id = tbl_payment.card_id "
                . " AND payment_method = 'cheque' "
                . " AND expenses_lvl1.expenses1_id = ex1_id "
                . " AND expenses_lvl2.expenses2_id = ex2_id ";
        //. " GROUP BY pet_id, expenses1_name, expenses2_name, cheque_no ";
        break;
    case "tbl_welfare":
        $sql = "SELECT sum(welfare) AS net "
                . " FROM tbl_welfare, tbl_payment, expenses_lvl1, expenses_lvl2 "
                . " WHERE approved_status = 'อนุมัติ' "
                . " AND pet_id IN('$val1') "
                . " AND tbl_welfare.id = tbl_payment.card_id "
                . " AND payment_method = 'cheque' "
                . " AND expenses_lvl1.expenses1_id = ex1_id "
                . " AND expenses_lvl2.expenses2_id = ex2_id ";
        break;
    default:
        break;
}

//echo $sql;

$db = new myDBS;
$db->selectDB("account");

$result1 = $db->runSQL($sql);
$row = $db->fetch_object($result1);

$bank = $_REQUEST['bank_name'];

if ($bank) {
    foreach ($bank as $b) {
        echo $b . "<br>";
    }
}
?>

<script type="text/javascript">
    $(document).ready(function() {
        $("#i_date").datepicker({
            dateFormat: "yy-mm-dd",
            showButtonPanel: true
        });
        
        $("#save_changes").click(function() {
            event.preventDefault();

            var i_date = $("#i_date").val();
            var val = "<?php echo $val; ?>";
            var payment_method = "<?php echo $payment_method; ?>";
            var tblname = "<?php echo $tblname; ?>";
            
            var bank = [];
            $("select[name^='bank_name[]']").each(function() {
                //alert($(this).val());
                bank.push($(this).val());
            });

            var branch = [];
            $("select[name^='branch_name[]']").each(function() {
                //alert($(this).val());
                branch.push($(this).val());
            });

            var bank_type = [];
            $("select[name^='bank_type[]']").each(function() {
                //alert($(this).val());
                bank_type.push($(this).val());
            });
            var a = [];
            $("input[name^='a[]']").each(function() {
                a.push($(this).val());
                //alert($(this).val());
            });
            
            var c = [];
            $("input[name^='c[]']").each(function() {
                c.push($(this).val());
                //alert($(this).val());
            });

            $.ajax({
                type: "POST",
                url: "pay_process",
                data: {bank: bank
                    , branch: branch
                    , bank_type: bank_type
                    , a: a
                    , c: c
                    , val: val
                    , payment_method: payment_method
                    , i_date: i_date
                    , tblname: tblname
                },
                cach: false,
                error: function() {
                    alert("ERROR !!!!");
                },
                success: function(data) {

                    window.location.href = "index.php?pages=cheque_new&tblname=" + tblname;
//                    alert(data);
//                    $("#test").html(data);
//                    if (confirm("คุณต้องการพิมพ์เช็คในตอนนี้ หรือไม่")) {
//                        window.open("cheque_print.php?cheque_no=" + cheque_no + "&tblname=" + tblname, "_blank");
//                        window.location.href = "index.php?pages=cheque&tblname=" + tblname;
//                    } else {


//                    }
                }
            });
        });
    });
</script>

<div id="test"></div>
<form action="index.php" >
    <input type="hidden" name="pages" value="cheque_save">
    <div class="row-fluid">
        <div class="control-group span6">
            <label class="control-label" for="จำนวนเงิน">จำนวนเงิน</label>
            <div class="controls">
                <input type="text" class="span12 text-right" id="amount" name="amount" value="<?php echo $row->net; ?>" readonly=""/>
            </div>
        </div>
        <div class="control-group span3">
            <label class="control-label" for="ผู้รับเงิน">ผู้รับเงิน</label>
            <div class="controls">
                <input type="text" class="span12" id="receiver" name="receiver" value="<?php echo $row->vender_name; ?>" readonly=""/>
            </div>
        </div>
        <div class="control-group span3">
            <label class="control-label" for="วันที่">วันที่</label>
            <div class="controls">
                <input class="span12" type="text" id="i_date" name="i_date" />

            </div>
        </div>
    </div>
    <div class="row-fluid">
        <div class="control-group span4">
            <label>ธนาคาร</label>
            <div class="controls">
                <select class="span12" id="bank_name" name="bank_name[]" >
                    <?php
                    $result = $db->showData("bank");
                    while ($rows = $db->fetch_object($result)) {
                        echo "<option value='$rows->bank_id'>$rows->bank_name</option>";
                    }
                    ?>
                </select>
            </div>
        </div>

        <div class="control-group span2">
            <label>สาขา</label>
            <div class="controls">
                <select class="span12" id="branch_name" name="branch_name[]" >
                    <?php
                    $result = $db->showData("bank_branch");

                    while ($rows = $db->fetch_object($result)) {
                        echo "<option value='$rows->bank_branch_id'>$rows->branch_name</option>";
                    }
                    ?>
                </select>
            </div>
        </div>
        <div class="control-group span2">
            <label>ประเภทบัญชี</label>
            <div class="controls">
                <select class="span12" id="bank_type" name="bank_type[]" >
                    <option value="">เลือก...</option>
                    <?php
                    $sql = "SELECT DISTINCT bank_type "
                            . " FROM bank_account "
                            . " ";
                    $result = $db->runSQL($sql);

                    while ($rows = $db->fetch_object($result)) {
                        echo "<option>$rows->bank_type</option>";
                    }
                    ?>
                </select>
            </div>
        </div>
        <div class="control-group span2">
            <label>เลขที่เช็ค/เลขที่บัญชี</label>
            <div class="controls">
                <input type="text" class="span12 " id="c" name="c[]">
            </div>
        </div>
        <div class="control-group span2">
            <label>จำนวนเงิน</label>
            <div class="controls">
                <input type="text" class="span12 text-right" id="a" name="a[]" value="<?php echo ($dis_multi) ? $row->net : ""; ?>">
            </div>
        </div>
    </div>

    <div class="row-fluid <?php echo ($dis_multi) ? " hide" : ""; ?>">
        <div class="control-group span4">
            <label>ธนาคาร</label>
            <div class="controls">
                <select class="span12" id="bank_name" name="bank_name[]" >
                    <?php
                    $result = $db->showData("bank");
                    while ($rows = $db->fetch_object($result)) {
                        echo "<option value='$rows->bank_id'>$rows->bank_name</option>";
                    }
                    ?>
                </select>
            </div>
        </div>

        <div class="control-group span2">
            <label>สาขา</label>
            <div class="controls">
                <select class="span12" id="branch_name" name="branch_name[]" >
                    <?php
                    $result = $db->showData("bank_branch");

                    while ($rows = $db->fetch_object($result)) {
                        echo "<option value='$rows->bank_branch_id'>$rows->branch_name</option>";
                    }
                    ?>
                </select>
            </div>
        </div>
        <div class="control-group span2">
            <label>ประเภทบัญชี</label>
            <div class="controls">
                <select class="span12" id="bank_type" name="bank_type[]" >
                    <option value="">เลือก...</option>
                    <?php
                    $sql = "SELECT DISTINCT bank_type "
                            . " FROM bank_account "
                            . " ";
                    $result = $db->runSQL($sql);

                    while ($rows = $db->fetch_object($result)) {
                        echo "<option>$rows->bank_type</option>";
                    }
                    ?>
                </select>
            </div>
        </div>
        <div class="control-group span2">
            <label>เลขที่เช็ค/เลขที่บัญชี</label>
            <div class="controls">
                <input type="text" class="span12" id="c" name="c[]">
            </div>
        </div>
        <div class="control-group span2">
            <label>จำนวนเงิน</label>
            <div class="controls">
                <input type="text" class="span12 text-right" id="a" name="a[]">
            </div>
        </div>
    </div>
    <div class="row-fluid <?php echo ($dis_multi) ? " hide" : ""; ?>">
        <div class="control-group span4">
            <label>ธนาคาร</label>
            <div class="controls">
                <select class="span12" id="bank_name" name="bank_name[]" >
                    <?php
                    $result = $db->showData("bank");
                    while ($rows = $db->fetch_object($result)) {
                        echo "<option value='$rows->bank_id'>$rows->bank_name</option>";
                    }
                    ?>
                </select>
            </div>
        </div>

        <div class="control-group span2">
            <label>สาขา</label>
            <div class="controls">
                <select class="span12" id="branch_name" name="branch_name[]" >
                    <?php
                    $result = $db->showData("bank_branch");

                    while ($rows = $db->fetch_object($result)) {
                        echo "<option value='$rows->bank_branch_id'>$rows->branch_name</option>";
                    }
                    ?>
                </select>
            </div>
        </div>
        <div class="control-group span2">
            <label>ประเภทบัญชี</label>
            <div class="controls">
                <select class="span12" id="bank_type" name="bank_type[]" >
                    <option value="">เลือก...</option>
                    <?php
                    $sql = "SELECT DISTINCT bank_type "
                            . " FROM bank_account "
                            . " ";
                    $result = $db->runSQL($sql);

                    while ($rows = $db->fetch_object($result)) {
                        echo "<option>$rows->bank_type</option>";
                    }
                    ?>
                </select>
            </div>
        </div>
        <div class="control-group span2">
            <label>เลขที่เช็ค/เลขที่บัญชี</label>
            <div class="controls">
                <input type="text" class="span12" id="c" name="c[]">
            </div>
        </div>
        <div class="control-group span2">
            <label>จำนวนเงิน</label>
            <div class="controls">
                <input type="text" id="a" class="span12 text-right" name="a[]">
            </div>
        </div>
    </div>
    <button class="btn btn-info" id="save_changes" name="save_changes">บันทึก</button>
</form>
